package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品基础属性接口")
@RestController
@RequestMapping("admin/product")
//@CrossOrigin
@RefreshScope //自动刷新配置
public class ManageController {

    @Autowired
    private ManageService manageService;

    @ApiOperation("查询一级分类")
    @GetMapping("getCategory1")
    public Result  getCategory1(){
      List<BaseCategory1> baseCategory1List= manageService.getCategory1();
      return Result.ok(baseCategory1List);
    }


    @ApiOperation("查询二级分类")
    @GetMapping("getCategory2/{category1Id}")
    public Result  getCategory2(@PathVariable Long category1Id){
        List<BaseCategory2> baseCategory2List= manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }


    @ApiOperation("查询三级分类")
    @GetMapping("getCategory3/{category2Id}")
    public Result  getCategory3(@PathVariable Long category2Id){
        List<BaseCategory3> baseCategory3List= manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    @ApiOperation("根据分类Id 获取平台属性集合")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id){
      List<BaseAttrInfo> baseAttrInfoList =manageService.attrInfoList(category1Id,category2Id,category3Id);
      return Result.ok(baseAttrInfoList);
    }

    @ApiOperation("保存-修改平台属性")
    @PostMapping("saveAttrInfo")
    public Result  saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    @ApiOperation("根据平台属性Id 获取到平台属性值集合")
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        //先查询平台属性存在不存在
        BaseAttrInfo baseAttrInfo= manageService.getAttrInfo(attrId);
        //在判断平台属性是否存在
        if (baseAttrInfo!=null){
           List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();
            return Result.ok(baseAttrValueList);
        }
        return Result.fail("暂无数据！");
    }

    @ApiOperation("获取销售属性数据")
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrs =manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrs);
    }

    @ApiOperation("保存spu")
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }


}
